import React, { useReducer } from "react";
// useReducer用来创建一个简易的仓库用的

// 创建仓库数据
const defaultState = {
  count: 1,
  name: "zhangsan",
};

// 创建同步改变的方法
const reducer = (state, action) => {
  switch (action.type) {
    case "add":
      return {
        ...state,
        count: state.count + action.n,
      };
    default:
      return state;
  }
};

const App = () => {
  const [state, dispatch] = useReducer(reducer, defaultState);

  const fn = () => {
    dispatch({
      type: "add",
      n: 3,
    });

    // store.commit("imcrement", {n: 3})
    // store.commit({
    //   type: "imcrement",
    //   n: 3
    // })
  };

  return (
    <>
      <h2>useReducer</h2>
      <p>count: {state.count}</p>
      <p>name: {state.name}</p>
      <button onClick={fn}>btn</button>
    </>
  );
};

export default App;
